from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, precision_score, recall_score, f1_score
import numpy as np
from python_ai.common.xcommon import sep

# load
sep('load')
cancer = load_breast_cancer()
x = cancer.data
y = cancer.target
m = len(x)
print(x[:5])
print(y[:5])

# scale
sep('scale')
xscaler = StandardScaler()
x = xscaler.fit_transform(x)
print(x[:5])

# shuffle
sep('shuffle')
rnd_idx = np.random.permutation(m)
x = x[rnd_idx]
y = y[rnd_idx]
print(x[:5])
print(y[:5])

# split
sep('split')
num = int(0.7 * m)
x_train, x_test = np.split(x, [num])
y_train, y_test = np.split(y, [num])
x = x_train
y = y_train

sep('fit')
model = LogisticRegression(solver='liblinear')
model.fit(x, y)
sep('theta')
theta0 = model.intercept_
theta1n = model.coef_
print(theta0.shape, theta1n.shape)
theta = np.c_[theta0, theta1n]
print(theta)

sep('score')
print(f'train score = {model.score(x, y)}')
print(f'test score = {model.score(x_test, y_test)}')

sep('predict')
h = model.predict(x)
print(h[:30])
h_test = model.predict(x_test)
print(h_test[:30])

sep('confusion matrix')
print('confusion mat', confusion_matrix(y, h))
print('classification report', classification_report(y, h))
print('accuracy', accuracy_score(y, h))
print('precision', precision_score(y, h))
print('recall', recall_score(y, h))
print('f1', f1_score(y, h))
